package rjgc.mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import rjgc.mall.common.MessageConstant;
import rjgc.mall.common.MySessionContext;
import rjgc.mall.common.Result;
import rjgc.mall.entity.AfterSale;
import rjgc.mall.entity.Seller;
import rjgc.mall.entity.User;
import rjgc.mall.mapper.AfterSaleMapper;
import rjgc.mall.service.AfterSaleService;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/AfterSale")
public class AfterSaleController {
    @Autowired
    private AfterSaleService afterSaleService;
    @Autowired
    private AfterSaleMapper afterSaleMapper;
    /**提交售后申请**/
    @RequestMapping("/publishAfterSaleService")
    public Result PublishAfterSaleService(@RequestHeader("token") String token,
                                          Integer goodid, String goodName,
                                          String description, @RequestParam("file") MultipartFile[] file) throws IOException {
        List<String> encodes = new ArrayList<>();
        BASE64Encoder base64Encoder = new BASE64Encoder();
        for(int i=0;i<file.length;i++){
            String encode = base64Encoder.encode(file[i].getBytes());
            encodes.add(encode);
        }
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(token);
        User user=(User) session.getAttribute("user");
        Integer userid = (Integer) user.getId();

        afterSaleService.publishAfterSaleService(userid,goodid,goodName,description,encodes);
        return new Result(true, "提交申请成功！");
    }
    /**查看售后申请**/
    @RequestMapping("/viewAfterSaleService")
    public List<AfterSale> ViewAfterSaleService(@RequestHeader("token") String token){
        MySessionContext myContext = MySessionContext.getInstance();
        HttpSession session = myContext.getSession(token);
        User user=(User) session.getAttribute("user");
        Integer userid = (Integer) user.getId();
        return afterSaleMapper.viewAfterSaleService(userid);
    }

}
